package com.spyx.product.controller;


import com.atyx.spyx.model.product.Category;
import com.atyx.spyx.vo.product.CategoryQueryVo;
import com.atyx.spyx.vo.product.CategoryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.product.service.CategoryService;
import com.spyx.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author yhp
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/admin/product/category")
@Api(tags = "商品分类管理")
//@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("分页+模糊查")  //分页+模糊查的方法，service中返回IPage类型,service层方法的参数: Page page,模糊查条件
    @GetMapping("/{page}/{limit}")
    public Result getPageList(
            @PathVariable("page")Integer page,
            @PathVariable("limit") Integer limit,
            CategoryQueryVo queryVo
    ){
        //调取service层
        Page p1 = new Page(page,limit);
        IPage<Category> pagemodel=categoryService.getPageList(p1,queryVo);
        return Result.ok(pagemodel);
    }
    @GetMapping("/get/{id}")
    @ApiOperation("主键查询")
    public Result getById(@PathVariable("id")Integer id){
        Category byId = categoryService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation("新增商品分类")
    @PostMapping("/save")
    public Result save(@RequestBody Category category){
        categoryService.save(category);
        return Result.ok();
    }

    @ApiOperation("修改商品分类")
    @PutMapping("/update")
    public Result update(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.ok();
    }

    @ApiOperation("删除单行")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id")Integer id){
        categoryService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result removeRows(@RequestBody List<Integer> idList){
        categoryService.removeByIds(idList);
        return Result.ok();
    }
    @ApiOperation("分类全查")
    @GetMapping("findAllList")
    public Result findAllList(){
        List<Category> list = categoryService.list();
        return Result.ok(list);
    }

}

